package com.ruijie.acl.task;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;

import com.ruijie.acl.AclPlanParameters;
import com.ruijie.acl.service.AclInterfaceService;
import com.ruijie.acl.service.AclPlanParametersService;
import com.ruijie.acl.task.config.TaskConfigAclDevice;
import com.ruijie.acl.task.model.AccessGroupInfo;
import com.ruijie.acl.task.model.AclInterfaceInfo;
import com.ruijie.acl.type.AclPlanTypeEnum;
import com.ruijie.acl.type.InterfaceAccessGroupTypeEnum;
import com.ruijie.acl.utils.AclConfigUtil;
import com.ruijie.acl.utils.AclUtil;
import com.ruijie.acl.web.config.Constants;
import com.ruijie.batch.core.factory.SpringConfigAware;
import com.ruijie.batch.plan.PlanParameters;
import com.ruijie.batch.plan.items.ItemParameters;
import com.ruijie.batch.task.ItemProcessResult;
import com.ruijie.emp.batch.task.AbstractDeviceExtendTasklet;
import com.ruijie.emp.system.service.TelnetTemplateService;

/**
 * 
 * <p>
 * Title: 删除ACL组,ACL部署计划
 * </p>
 * <p>
 * Description: Function Description
 * </p>
 * <p>
 * Copyright: Copyright (c) 2009
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time: 2009-09-27
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p>
 * 
 * @author ASAI
 */
public class DeleteAccessGroupTask extends AbstractDeviceExtendTasklet implements SpringConfigAware {

	private Log logger = LogFactory.getLog(DeleteAccessGroupTask.class);

	private TelnetTemplateService service;

	private AclPlanParametersService aclPlanParametersService;

	private AclInterfaceService aclInterfaceService;

	private TaskConfigAclDevice taskConfigAclDevice;

	private Map<String, String> paramMap = new HashMap<String, String>();

	protected Map<String, String> parseParameters(ItemParameters itemParameters) {
		try {
			// paramMap = new HashMap<String, String>();
			// String id =
			// itemParameters.getParameterValue(Constants.ACL_DEPLOY_PLAN_ITEM_PARAM_NAME);
			// p.put(Constants.ACL_DEPLOY_PLAN_ITEM_PARAM_NAME, id);
			return paramMap;
		} catch (Throwable error) {
			// do nothing
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public ItemProcessResult processItem(Object item, ItemParameters itemParameters) {
		ItemProcessResult result = new ItemProcessResult();
		paramMap.clear();
		Assert.notNull(item, "item is null");

		PlanParameters planParameters = new PlanParameters();

		// 取得参数
		String aclPlanParamtersId = itemParameters.getParameterValue(Constants.ACL_DEPLOY_PLAN_ITEM_PARAM_NAME);
		AclPlanParameters aclPlanParamters = aclPlanParametersService.get(Long.parseLong(aclPlanParamtersId));
		String accessGroupNames = aclPlanParamters.getAccessGroups();
		// 取得要删除的组信息
		if (!AclUtil.empty(aclPlanParamters.getAccessGroups())) {
			// accessGroupNames =
			// AclUtil.unescapeSpecialString(accessGroupNames);
			List<AccessGroupInfo> accessGroupInfoList = AclConfigUtil.string2AccessGroupInfo(accessGroupNames);
			planParameters.putParameter("accessGroupInfoList", accessGroupInfoList);
			this.paramMap.put(Constants.ACL_DEPLOY_PLAN_ITEM_PARAM_NAME_GROUPNAMES, AclConfigUtil
					.convertAccessGroupNames(accessGroupInfoList));

			// 取得与时间段相关的组信息
			if (!AclUtil.empty(aclPlanParamters.getInterfaces())) {
				List<AclInterfaceInfo> aclInterfaceInfoList = convertName2AclInterfaceInfo(aclPlanParamters
						.getInterfaces());
				if (aclInterfaceInfoList != null && aclInterfaceInfoList.size() > 0) {
					planParameters.putParameter("aclInterfaceInfoList", aclInterfaceInfoList);
					this.paramMap.put(Constants.ACL_DEPLOY_PLAN_ITEM_PARAM_NAME_INTERFACES, AclConfigUtil
							.convertInterfaceStrings(aclInterfaceInfoList));
				}

			}
			// 调用下发
			planParameters.putParameter("dealFlag", AclPlanTypeEnum.DeleteAccessGroup.toString());
			
			return taskConfigAclDevice.processItem(item, planParameters);
		} else {
			logger.warn("要删除的组信息列表为空！");
		}

		// 如果为删除ACL组
		return result;
	}

	/**
	 * 把字符串转换成计划接口类型
	 * 
	 * @param planInterfaceString
	 * @return
	 */
	private List<AclInterfaceInfo> convertName2AclInterfaceInfo(String planInterfaceString) {
		List<AclInterfaceInfo> aclInterfaceList = null;
		if (!AclUtil.empty(planInterfaceString)) {
			aclInterfaceList = new ArrayList<AclInterfaceInfo>();
			String[] arrAclPlanInterfaceList = planInterfaceString.split(";");
			for (String planInterfaceStr : arrAclPlanInterfaceList) {
				AclInterfaceInfo aclPlanInterface = new AclInterfaceInfo();
				String[] arrAclPlanInterface = planInterfaceStr.split(",");
				if (arrAclPlanInterface.length == 3) {
					aclPlanInterface.setIfName(arrAclPlanInterface[0]);
					String accessGroupType = arrAclPlanInterface[1];
					String accessGroupName = AclUtil.unescapeSpecialString(arrAclPlanInterface[2]);
					if (accessGroupType.equals(InterfaceAccessGroupTypeEnum.In.toString())) {
						aclPlanInterface.setInAccessGroupName(accessGroupName);

					} else if (accessGroupType.equals(InterfaceAccessGroupTypeEnum.Out.toString())) {
						aclPlanInterface.setOutAccessGroupName(accessGroupName);

					} else if (accessGroupType.equals(InterfaceAccessGroupTypeEnum.InOut.toString())) {
						aclPlanInterface.setInAccessGroupName(accessGroupName);
						aclPlanInterface.setOutAccessGroupName(accessGroupName);
					}
				}
				aclInterfaceList.add(aclPlanInterface);
			}
		}
		return aclInterfaceList;
	}

	public void setService(TelnetTemplateService service) {
		this.service = service;
	}

	public void setAclInterfaceService(AclInterfaceService aclInterfaceService) {
		this.aclInterfaceService = aclInterfaceService;
	}

	public AclPlanParametersService getAclPlanParametersService() {
		return aclPlanParametersService;
	}

	public void setAclPlanParametersService(AclPlanParametersService aclPlanParametersService) {
		this.aclPlanParametersService = aclPlanParametersService;
	}

	public TaskConfigAclDevice getTaskConfigAclDevice() {
		return taskConfigAclDevice;
	}

	public void setTaskConfigAclDevice(TaskConfigAclDevice taskConfigAclDevice) {
		this.taskConfigAclDevice = taskConfigAclDevice;
	}

	public TelnetTemplateService getService() {
		return service;
	}

	public AclInterfaceService getAclInterfaceService() {
		return aclInterfaceService;
	}

}
